//
//  SpeedingVC.m
//  NotepadApp
//
//  Created by yobet on 2018/4/16.
//  Copyright © 2018年 HJ. All rights reserved.
//

@interface SpeedingCell : UICollectionViewCell

@property(nonatomic,strong) UILabel * title;
@property(nonatomic,strong) UIImageView * mainImage;
@end

@implementation SpeedingCell

-(UIImageView *)mainImage {
    if (!_mainImage) {
        _mainImage = [[UIImageView alloc]init];
        _mainImage.contentMode = UIViewContentModeScaleToFill;
        _mainImage.image =[UIImage imageNamed:@"Shoppingcart"];
        [self addSubview:_mainImage];
    }
    return _mainImage;
}
-(UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc]init];
        _title.textColor =HJColorRGB(0);
        _title.font = [UIFont systemFontOfSize:13];
        _title.textAlignment = NSTextAlignmentCenter;
        _title.text= @"123";
        [self addSubview:_title];
    }
    return _title;
}


-(void)layoutSubviews {
    [super layoutSubviews];
    self.mainImage.frame = CGRectMake(self.width/2-(self.width/2), 0, self.width, 50);
    self.title.frame = CGRectMake(0, 40, self.width, 15);
}

@end


#import "SpeedingVC.h"
#import "SpenedingCell.h"
#import "JCKeyBoardNum.h"
#import "UITextField+JCTextField.h"
#import "IQKeyboardManager.h"
#import <IQKeyboardReturnKeyHandler.h>
//#import "HomeKeepModel.h"

static NSString * const identifier = @"cell";

@interface SpeedingVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>{
    IQKeyboardReturnKeyHandler * _returnKeyHander;
}
@property(nonatomic,strong) UICollectionView * collectionView;
@property(nonatomic,strong) UITextField * num_TF;
@property (nonatomic, strong) JCKeyBoardNum *NumKeyBoard;
@property(nonatomic,strong) NSMutableArray *leftArray;
@property(nonatomic,strong) NSMutableArray *leftSelecteArray;
@property(nonatomic,strong) NSMutableArray *rightSelecteArray;
@property(nonatomic,strong) NSMutableArray *rightArray;
@property(nonatomic,assign) BOOL isSegment;
@property(nonatomic,strong) NSData * imageData;
@property(nonatomic,strong) NSString * content;
@property(nonatomic,assign) NSInteger tag;
@end

@implementation SpeedingVC

-(NSMutableArray *)leftArray {
    if (!_leftArray) {
        _leftArray =@[@{@"image":@"office",@"title":@"办公"},@{@"image":@"lottery",@"title":@"彩票"},@{@"image":@"catering",@"title":@"餐饮"},@{@"image":@"pet",@"title":@"宠物"},@{@"image":@"communication",@"title":@"沟通"},@{@"image":@"shop",@"title":@"购物"},@{@"image":@"kid",@"title":@"孩子"},@{@"image":@"communication",@"title":@"交通"},@{@"image":@"living",@"title":@"居家"},@{@"image":@"donate",@"title":@"捐赠"},@{@"image":@"cash gift ",@"title":@"礼金"},@{@"image":@"gift",@"title":@"礼物"},@{@"image":@"snacks",@"title":@"零食"},@{@"image":@"travel",@"title":@"旅行"},@{@"image":@"hairdressing",@"title":@"美容"}, @{@"image":@"car",@"title":@"汽车"},@{@"image":@"commodity",@"title":@"商品"},@{@"image":@"set",@"title":@"设置"}, @{@"image":@"social contact",@"title":@"社交"},@{@"image":@"income",@"title":@"收入"},@{@"image":@"book",@"title":@"书籍"}, @{@"image":@"vegetables",@"title":@"蔬菜"},@{@"image":@"numerical code",@"title":@"数码"},@{@"image":@"fruits",@"title":@"水果"}, @{@"image":@"maintain",@"title":@"维修"},@{@"image":@"cigarette",@"title":@"烟酒"},@{@"image":@"clothing ",@"title":@"衣服"},@{@"image":@"medical treatment",@"title":@"医疗"},@{@"image":@"recreation",@"title":@"娱乐"},@{@"image":@"exercise ",@"title":@"运动"},@{@"image":@"elder",@"title":@"长辈"},@{@"image":@"housing",@"title":@"住房"},@{@"image":@"study",@"title":@"学习"}
                ].mutableCopy;
    }
    return _leftArray;
}

-(NSMutableArray *)rightArray {
    if (!_rightArray) {
        _rightArray =@[@{@"image":@"salary",@"title":@"工资"},@{@"image":@"part-time job",@"title":@"兼职"},
                       @{@"image":@"cash gift",@"title":@"礼金"},@{@"image":@"manage money matters",@"title":@"理财"},
                       @{@"image":@"else",@"title":@"其他"},@{@"image":@"setting up",@"title":@"设置"}
                       ].mutableCopy;
    }
    return _rightArray;
}

-(NSMutableArray *)leftSelecteArray {
    if (!_leftSelecteArray) {
        _leftSelecteArray = @[@"office1",@"lottery1",@"catering1",@"pet1",@"communication1",@"shop1",@"kid1",@"communication2",@"living1",@"donate1",@"cash gift 1",@"gift1",@"snacks1",@"travel1",@"hairdressing1",@"car1",@"commodity1",@"set1",@"social contact1",@"income1",@"book1",@"vegetables1",@"numerical code1",@"fruits1",@"maintain1",@"cigarette1",@"clothing 1",@"medical treatment1",@"recreation1",@"exercise 1",@"elder1",@"housing1",@"study1"].mutableCopy;
    }
    return _leftSelecteArray;
}

-(NSMutableArray *)rightSelecteArray {
    if (!_rightSelecteArray) {
        _rightSelecteArray = @[@"salary1",@"part-time job1",@"cash gift1",@"manage money matters1",@"else1",@"setting up1"].mutableCopy;
    }
    return _rightSelecteArray;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _returnKeyHander = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftNavImageName = @"back";
    self.isSegment = YES;
    self.tag= 1;
    [self configSegmentWithleftTitle:@"支出" rightTitle:@"收入"];
    [self configCollectionView];
}
-(void)didClickLeftNavAction {
    [mNotificationCenter postNotificationName:@"speedingNotification" object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)segmentCilik:(id)sender {
    switch ([sender selectedSegmentIndex]) {
        case 0:
            self.isSegment = YES;
            self.tag = 1;
            break;
        case 1:
            self.tag = 2;
            self.isSegment = NO;
            break;
        default:
            break;
    }
    [self.collectionView reloadData];
}
-(void)configCollectionView {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((mScreen_W-70)/4, (mScreen_W-70)/4);
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, mScreen_W, mScreen_H-(NavBar_H)-70) collectionViewLayout:layout];
    self.collectionView.showsVerticalScrollIndicator =NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"SpenedingCell" bundle:nil] forCellWithReuseIdentifier:identifier];
    [self.view addSubview:self.collectionView];
    
    UIView * btmView =[[UIView alloc]initWithFrame:CGRectMake(0, mScreen_H-(NavBar_H)-70, mScreen_W, 70)];
    btmView.backgroundColor = HJColorRGB(243);
    [self.view addSubview:btmView];
    [self.view bringSubviewToFront:btmView];
    
    self.num_TF = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, btmView.width-40, 50)];
    self.num_TF.delegate = self;
    self.num_TF.placeholder = @"请输入内容。。。";
    self.num_TF.clearsOnBeginEditing = YES;
    self.num_TF.borderStyle = UITextBorderStyleRoundedRect;
    [btmView addSubview:self.num_TF];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.isSegment ? self.leftArray.count : self.rightArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SpenedingCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!self.isSegment) {
        cell.titleLabel.text = self.rightArray[indexPath.row][@"title"];
        cell.mainImage.image = [UIImage imageNamed:self.rightArray[indexPath.row][@"image"]];
    }else {
        cell.titleLabel.text = self.leftArray[indexPath.row][@"title"];
        cell.mainImage.image = [UIImage imageNamed:self.leftArray[indexPath.row][@"image"]];
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SpenedingCell * cell= (SpenedingCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (!self.isSegment) {
        cell.mainImage.image =[UIImage imageNamed:self.rightSelecteArray[indexPath.row]];
    }else {
        cell.mainImage.image =[UIImage imageNamed:self.leftSelecteArray[indexPath.row]];
    }
    self.imageData = UIImagePNGRepresentation(cell.mainImage.image);
    self.content = cell.titleLabel.text;
    [self.num_TF becomeFirstResponder];
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    SpenedingCell * cell= (SpenedingCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (!self.isSegment) {
        cell.mainImage.image =[UIImage imageNamed:self.rightArray[indexPath.row][@"image"]];
    }else {
        cell.mainImage.image =[UIImage imageNamed:self.leftArray[indexPath.row][@"image"]];
    }
    self.imageData = NULL;
    self.content = @"";
}
#pragma mark -- 插入数据库
-(void)insertFMDBWithlist:(NSString *)content image:(NSData *)image time:(NSString *)time money:(NSString *)money tag:(NSInteger)tag {
    if ([self.homeDB open]) {
//        (content text, image blob, time text, money text, tag integer)
        [self.homeDB executeUpdateWithFormat:@"INSERT INTO homeSpeeding (content, image, time, money, tag) VALUES (%@, %@, %@, %@, %ld);", content,image,time, money, (long)tag];
    }
    [self.homeDB close];
}

#pragma mark====== UITextFieldDelegate 代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.NumKeyBoard = [JCKeyBoardNum show];
    textField.inputView = self.NumKeyBoard;
    __weak typeof(self) weakSelf = self;
    //点击键盘
    self.NumKeyBoard.completeBlock = ^(NSString *text,NSInteger tag) {
        switch (tag) {
            case 9:
                //点击完成按钮
                [weakSelf.view endEditing:YES];
                if ([weakSelf.imageData isKindOfClass:[NSNull class]]) {
                    return [ProgressHUD show:@"请选择类型"];
                }
                if ([weakSelf.num_TF.text isEqualToString:@""]) {
                    return [ProgressHUD show:@"请输入内容"];
                }
                [weakSelf insertFMDBWithlist:weakSelf.content image:weakSelf.imageData time:[CommonTools getCurrentTimes:YES isYear:YES] money:weakSelf.num_TF.text tag:weakSelf.tag];
                [weakSelf didClickLeftNavAction];
                break;
            case 11:
                //点击删除按钮
                [weakSelf clickDeleteBtn];
                break;
            default:
                //点击数字键盘
                [weakSelf.num_TF changTextWithNSString:text];
                break;
        }
    };
    return YES;
}
- (void)clickDeleteBtn {
    if (self.num_TF.text.length > 0) {
        self.num_TF.text = [self.num_TF.text substringToIndex:self.num_TF.text.length - 1];
    }
}
@end
